library navigation;

import 'dart:html';
import 'dart:math' as math;

num hdg_now = 30;
num hdg_target = 90;
num centerX, centerY;
const MAX_D = 300;
String color = 'gree';

void main() {
  CanvasElement canvas = query("#canvas");
  centerX = centerY = MAX_D / 2;
  var context = canvas.context2d;

  InputElement target2 = query("#target2");
  target2.on.change.add((Event e) {
    hdg_target = int.parse(target2.value);
    if(hdg_target == 360) {
      target2.value = '0';
    } else if(hdg_target == 0) {
      target2.value = '360';
    }
    query("#t").text = hdg_target.toString();
    drawFrame(context);
  }, true);

  InputElement now2 = query("#now2");
  now2.on.change.add((Event e) {
    hdg_now = int.parse(now2.value);
    if(hdg_now == 360) {
      now2.value = '0';
    } else if ( hdg_now == 0 ) {
      now2.value = '360';
    }
    query("#n").text = hdg_now.toString();
    drawFrame(context);
  }, true);

  drawFrame(context);
}

void drawFrame(CanvasRenderingContext2D context) {
  context.clearRect(0, 0, MAX_D, MAX_D);

  num n1, n2;
  num diff = hdg_target - hdg_now;
  num obs = n1 = n2 = hdg_target;
  if (diff < -180) {
    diff = 360+diff;
  } else if ( diff > 180) {
    diff = -(360-diff);
  }
  // Zuerst in Richtung n1
  // Dann bei Bedarf noch in Richtung n2
  // Zuletzt in Richtung target
  if ( diff.abs() < 15 ) {
    n1 = (n1 + (diff > 0 ? -30 : 30)) % 360;
  } else if (diff.abs()< 30) {
    n1 = (n1 + (diff > 0 ? -45 : 45)) % 360;
  } else if (diff.abs() > 165) {
    n1 = (n1 + (diff > 0 ? -30 : 30)) % 360;
  } else if (diff.abs() > 150) {
    n1 = (n1 + (diff > 0 ? -45 : 45)) % 360;
  } else {
    // OBS zeigt an, ab wann ein 45 Grad Anflug beginnt
    if( diff.abs() > 90) {
      obs = (obs + (diff > 0 ? -160 : 160)) % 360;
    } else {
      obs = (obs + (diff > 0 ? -20 : 20)) % 360;
    }
    n1 = (n1 + (diff > 0 ? -90 : 90)) % 360;
    n2 = (n2 + (diff > 0 ? -45 : 45)) % 360;
  }
  query("#diff").text = "${diff}";
  query("#n1").text = n1.toString();
  query("#obs").text = obs.toString();
  query("#n2").text = n2.toString();

  // Zielrichtung
  context.beginPath();
  context.lineWidth = 2;
  context.strokeStyle = "green";
  context.moveTo(MAX_D/2,MAX_D/2);
  var radians = (hdg_target-90) * (math.PI / 180);
  var x = math.cos(radians)*150+MAX_D/2;
  var y = math.sin(radians)*150+MAX_D/2;
  context.lineTo(x,y);
  context.stroke();
  context.closePath();

  // Aktuelle Richtung
  context.beginPath();
  context.lineWidth = 2;
  context.strokeStyle = "red";
  context.moveTo(MAX_D/2,MAX_D/2);
  radians = (hdg_now+90) * (math.PI / 180);
  var pos_x = math.cos(radians) * 120 + MAX_D/2;
  var pos_y = math.sin(radians) * 120 + MAX_D/2;
  context.lineTo(pos_x,pos_y);
  context.stroke();
  context.closePath();

  // n1
  radians = (n1-90) * (math.PI / 180);
  x = math.cos(radians) * 50 + pos_x;
  y = math.sin(radians) * 50 + pos_y;
  context.beginPath();
  context.strokeStyle = "blue";
  context.moveTo(pos_x, pos_y);
  context.lineTo(x,y);
  context.stroke();
  context.closePath();

  // OBS bei 90/45 Regel
  if(n2 != hdg_target) {
    // n2
    context.beginPath();
    context.strokeStyle = "lightblue";
    context.moveTo(x,y);
    radians = (n2-90) * (math.PI / 180);
    x += math.cos(radians) * 30;
    y += math.sin(radians) * 30;
    context.lineTo(x,y);
    context.stroke();
    context.closePath();

    // OBS
    radians = (obs+90) * (math.PI / 180);
    x = math.cos(radians) * 150 + MAX_D / 2;
    y = math.sin(radians) * 150 + MAX_D / 2;
    context.beginPath();
    context.strokeStyle = "orange";
    context.moveTo(MAX_D/2, MAX_D/2);
    context.lineTo(x,y);
    context.stroke();
    context.closePath();
  }
}